package net.alche.al.util.far;

import java.util.Timer;
import java.util.TimerTask;

import net.alche.al.util.Far;
import net.alche.al.util.far.pojo.FaLogicTimeMoreInfo;
import net.alche.al.util.far.pojo.VueFunction;
import net.alche.al.util.far.pojo.ext.ThreadLocalNode;

// FaBaseLogic
@SuppressWarnings("all")
public class FaBaseLogic {
  private static final ThreadLocal<ThreadLocalNode> LOCAL_ONOCE = new ThreadLocal<>();

  public FaBaseLogic() {
  }

  public ThreadLocalNode popThrowdData() {
    ThreadLocalNode node = LOCAL_ONOCE.get();
    if (node != null) {
      LOCAL_ONOCE.remove();
    }
    return node;
  }

  public void setThreadData(Object obj) {
    LOCAL_ONOCE.set(new ThreadLocalNode(obj));
  }

  public void threadSleep(int millx) {
    try {
      Thread.sleep(millx);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public VueFunction timeout(int ms) {
    return Far.http.timeOut(ms);
  }

  public VueFunction newTimer(int ms) {
    VueFunction fun = new VueFunction();
    FaLogicTimeMoreInfo tmi = new FaLogicTimeMoreInfo();
    Timer tim = new Timer();
    tim.schedule(new TimerTask() {
      @Override
      public void run() {
        fun.callVals(tmi);
        tmi.numIndex++;
        if (tmi.stop) {
          cancel();
        }
      }
    }, ms, ms);
    return fun;
  }
}